package com.lianbang.pharmacy.module.user

import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer

import com.lianbang.pharmacy.R
import cn.ubest.mvvmcore.base.BaseListActivity
import cn.ubest.mvvmcore.interfaces.IEmptyView
import cn.ubest.mvvmcore.utils.DialogUtils
import cn.ubest.mvvmcore.utils.DoubleClickHelper
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lianbang.pharmacy.adapter.MyAddressListAdapter
import com.lianbang.pharmacy.bean.AddressListBean
import com.lianbang.pharmacy.databinding.ActivityMyAddressBinding
import com.lianbang.pharmacy.vm.AddressVM
import com.lianbang.pharmacy.vm.MainVM

class MyAddressActivity : BaseListActivity<AddressVM, ActivityMyAddressBinding, AddressListBean>(),
    IEmptyView {

    companion object {
        fun start(context: Context?) {
            context?.also {
                val starter = Intent(it, MyAddressActivity::class.java)
                if (it !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                it.startActivity(starter)
            }

        }
    }

    override fun initLayout(): Int {
        return R.layout.activity_my_address;

    }

    override fun initData(intent: Intent) {
        super.initData(intent)
        mViewModel.mAddressLiveData.observe(this, Observer {
            if (it is Int && it >= 0 && it < mListAdapter.data.size) {
                if (it == 0) {
                    mListAdapter.data.removeAt(0)
                    mListAdapter.notifyDataSetChanged()
                } else {
                    mListAdapter.remove(it)
                }
            }
        })
    }

    override fun initView() {
        super.initView()
        mListAdapter.setOnItemChildClickListener { adapter, view, position ->
            val addressListBean = adapter.data[position] as AddressListBean
            if (view.id == R.id.btn_edit) {
                EditAddressActivity.start(this@MyAddressActivity, addressListBean)
            } else if (view.id == R.id.btn_delete) {
                DialogUtils.showDialog(
                    this, "是否要删除该地址"
                ) { _, _ ->
                    addressListBean.id?.also {
                        mViewModel.deleteAddress(it, position)
                    }
                }
            }
        }
    }

    override fun loadData() {
        mViewModel.refreshData()
    }

    override fun initAdapter(): BaseQuickAdapter<AddressListBean, *> {
        return MyAddressListAdapter()
    }

    override fun setToolbarTitle(): String? {
        return "收货地址"
    }

    override fun getEmptyView(): View {
        val v = layoutInflater.inflate(R.layout.empty_my_address_list, null)
        v.findViewById<ImageView>(R.id.iv_img).setImageResource(R.drawable.empty_my_address)
        v.findViewById<TextView>(R.id.tv_text).text = "收货地址为空～"
        v.findViewById<View>(R.id.btn_add_address)
            .setOnClickListener(object : DoubleClickHelper.OnClickListener() {
                override fun onClicked(view: View?) {
                    EditAddressActivity.start(this@MyAddressActivity, null)
                }
            })
        return v
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == EditAddressActivity.REQ_CODE) {
                loadData()
            }
        }
    }
}
